<?php
namespace app\admin\controller;

class BlockArea extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'BlockArea';
    }
    //
    protected function _before_index(){
        $thead = [
            'baid' => 'ID',
            'area' => '地区名称',
            'use_domain' => '用于域名屏蔽',
            'use_advert' => '用于广告计划',
            'used' => '使用状态',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _filter($map){
        return $map;
    }
    //
    protected function _after_list($list){
        foreach($list as $key => $row){
            $list[$key]['use_domain'] = $row['use_domain'] == 1 ? '是' : '否';
            $list[$key]['use_advert'] = $row['use_advert'] == 1 ? '是' : '否';
            $list[$key]['used'] = $row['used'] == 1 ? '在用' : '未用';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){}
    //
    protected function _before_insert($data){
        $data['area'] = str_replace(['省','市','区'], '', $data['area']);
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $id){
        $this->log(['pk' => 'baid', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){}
    //
    protected function _filter_edit($list){
        return $list;
    }
    //
    protected function _before_save($data){
        if(!isset($data['use_domain'])){
            $data['use_domain'] = 0;
        }
        if(!isset($data['use_advert'])){
            $data['use_advert'] = 0;
        }
        $data['area'] = str_replace(['省','市','区'], '', $data['area']);
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'baid', 'value' => $data['baid']], $data);
    }
    //
    protected function _filter_del(){
        return [1,2,6,7];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'baid', 'value' => $row['baid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}